package com.hhf.rpa.job.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hhf.rpa.base.ResponseResult;
import com.hhf.rpa.job.config.BaseJobConfig;
import com.hhf.rpa.job.entity.BaseJob;
import com.hhf.rpa.job.entity.BaseJobLog;
import com.hhf.rpa.job.pojo.form.BaseJobForm;
import com.hhf.rpa.job.service.BaseJobHandler;
import com.hhf.rpa.job.service.BaseJobLogService;
import com.hhf.rpa.job.service.BaseJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Controller
@RequestMapping("/admin/job")
public class JobAdminController {
    @Autowired
    private BaseJobConfig baseJobConfig;
    @Autowired
    private BaseJobService baseJobService;
    @Autowired
    private BaseJobLogService baseJobLogService;
    @Autowired
    private BaseJobHandler baseJobHandler;

    @GetMapping("/getJobPage")
    public String getJobPage(HttpServletRequest request, BaseJobForm form) {
        IPage<BaseJob> page = baseJobService.getJobPage(form);
        request.setAttribute("page", page);
        return "/baseJobPage";
    }

    @GetMapping("/baseJobEdit")
    public String baseJobEdit(HttpServletRequest request, Integer jobId) {
        BaseJob job = baseJobService.getJobById(jobId);
        request.setAttribute("job",job);
        return "/baseJobEdit";
    }
    @GetMapping("/addJob")
    public String baseJobEdit(HttpServletRequest request) {
        request.setAttribute("job",new BaseJob());
        return "/baseJobEdit";
    }
    @PostMapping("/saveBaseJob")
    public String saveBaseJob(BaseJob baseJob) {
        baseJobService.saveJob(baseJob);
        baseJobConfig.refreshAll();
        return "redirect:/admin/job/getJobPage";
    }
    @PostMapping("/delJob")
    @ResponseBody
    public ResponseResult delJob(HttpServletRequest request, Integer jobId) {
        baseJobService.removeById(jobId);
        baseJobConfig.refreshAll();
        IPage<BaseJob> page = baseJobService.getJobPage(new BaseJobForm());
        request.setAttribute("page", page);
        return ResponseResult.success();
    }
    @PostMapping("/baseJobCopy")
    @ResponseBody
    public ResponseResult baseJobCopy(HttpServletRequest request, Integer jobId) {
        baseJobService.copyJob(jobId);
        baseJobConfig.refreshAll();
        IPage<BaseJob> page = baseJobService.getJobPage(new BaseJobForm());
        request.setAttribute("page", page);
        return ResponseResult.success();
    }
    @RequestMapping("/getJobLogDetail")
    public String getJobLogDetail(HttpServletRequest request, Long logId) {
        Assert.notNull(logId, "logId为空");
        BaseJobLog baseJobLog = baseJobLogService.getJobLogByLogId(logId);
        request.setAttribute("baseJobLog", baseJobLog);
        return "/baseJobLogDetail";
    }

    @GetMapping("/getJobLogPage")
    public String getJobLogPage(HttpServletRequest request, BaseJobForm form) {
        Page<BaseJobLog> jobLogPage = baseJobLogService.getJobLogPage(form);
        request.setAttribute("page", jobLogPage);
        return "/baseJobLogPage";
    }
    /**
     * 刷新定时任务配置生效
     * @return
     */
    @RequestMapping("/task/refresh")
    @ResponseBody
    public ResponseResult refreshTask() {
        baseJobConfig.refresh();
        return ResponseResult.success();
    }

    /**
     * 手动执行任务
     * @param jobId
     * @return
     * @throws ClassNotFoundException
     */
    @PostMapping("/task/execTask")
    @ResponseBody
    public ResponseResult execTask(Integer jobId) {
        baseJobConfig.refreshAll();
        BaseJob baseJob = new BaseJob();
        baseJob.setJobId(jobId);
        baseJobHandler.execHandler(baseJob,2);
        return ResponseResult.success();
    }
}
